﻿Imports System.Xml
Imports System.IO

Public Class frmPersonal
    Dim xmldoc As New XmlDocument
    Dim dataTable As DataTable = New DataTable("Personal")
    Dim dataRow As DataRow
    Dim filePath As String = ""

    Private Sub ButtonCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCreate.Click
        AddDataToTable()
        LoadGridView()
    End Sub

    'Add data enter from textbox to table
    Private Sub AddDataToTable()
        dataRow = dataTable.NewRow()
        dataRow.Item("VietNameseName") = RichTextBox_VietNameseName.Text
        dataRow.Item("EnglishName") = RichTextBox_EnglishName.Text
        dataRow.Item("BirthDay") = DateTimePicker_BirthDay.Text
        dataRow.Item("WhereBorn") = RichTextBox_WhereBorn.Text
        dataRow.Item("Lives") = RichTextBox_Lives.Text
        dataRow.Item("AccountBank") = RichTextBox_AccountBank.Text
        dataRow.Item("Email") = RichTextBox_Email.Text
        dataRow.Item("Yahoo") = RichTextBox_Yahoo.Text
        dataRow.Item("Skype") = RichTextBox_Skype.Text
        dataRow.Item("Phone") = RichTextBox_Phone.Text
        dataRow.Item("Skills") = RichTextBox_Skills.Text
        dataRow.Item("Favorist") = RichTextBox_Favorist.Text
        dataRow.Item("Working") = RichTextBox_Working.Text
        dataRow.Item("Learn") = RichTextBox_Learn.Text
        dataRow.Item("AnortherInformation") = RichTextBox_AnortherInformation.Text
        dataRow.Item("Status") = ComboBox_Status.SelectedItem.ToString()
        dataTable.Rows.Add(dataRow)
    End Sub

    'Load data for GridView
    Private Sub LoadGridView()
        DataGridView_Personal.DataSource = dataTable
    End Sub

    'Create table save data what customer input
    Private Sub CreateDataTable()
        Dim VietNameseName As DataColumn = New DataColumn("VietNameseName", Type.GetType("System.String"))
        Dim EnglishName As DataColumn = New DataColumn("EnglishName", Type.GetType("System.String"))
        Dim BirthDay As DataColumn = New DataColumn("BirthDay", Type.GetType("System.String"))
        Dim WhereBorn As DataColumn = New DataColumn("WhereBorn", Type.GetType("System.String"))
        Dim Lives As DataColumn = New DataColumn("Lives", Type.GetType("System.String"))
        Dim AccountBank As DataColumn = New DataColumn("AccountBank", Type.GetType("System.String"))
        Dim Email As DataColumn = New DataColumn("Email", Type.GetType("System.String"))
        Dim Skype As DataColumn = New DataColumn("Skype", Type.GetType("System.String"))
        Dim Yahoo As DataColumn = New DataColumn("Yahoo", Type.GetType("System.String"))
        Dim Phone As DataColumn = New DataColumn("Phone", Type.GetType("System.String"))
        Dim Skills As DataColumn = New DataColumn("Skills", Type.GetType("System.String"))
        Dim Favorist As DataColumn = New DataColumn("Favorist", Type.GetType("System.String"))
        Dim Working As DataColumn = New DataColumn("Working", Type.GetType("System.String"))
        Dim Learn As DataColumn = New DataColumn("Learn", Type.GetType("System.String"))
        Dim AnortherInformation As DataColumn = New DataColumn("AnortherInformation", Type.GetType("System.String"))
        Dim Status As DataColumn = New DataColumn("Status", Type.GetType("System.String"))
        dataTable.Columns.Add(VietNameseName)
        dataTable.Columns.Add(EnglishName)
        dataTable.Columns.Add(BirthDay)
        dataTable.Columns.Add(WhereBorn)
        dataTable.Columns.Add(Lives)
        dataTable.Columns.Add(AccountBank)
        dataTable.Columns.Add(Email)
        dataTable.Columns.Add(Skype)
        dataTable.Columns.Add(Yahoo)
        dataTable.Columns.Add(Phone)
        dataTable.Columns.Add(Skills)
        dataTable.Columns.Add(Favorist)
        dataTable.Columns.Add(Working)
        dataTable.Columns.Add(Learn)
        dataTable.Columns.Add(AnortherInformation)
        dataTable.Columns.Add(Status)
    End Sub

    Private Sub frmPersonal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateDataTable()
    End Sub

    Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
        Dim SaveFileDialog As New SaveFileDialog
        SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        SaveFileDialog.Filter = "Text Files (*.ns)|*.ns|All Files (*.*)|*.*"
        If filePath.Equals("") Then
            If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
                Dim FileName As String = SaveFileDialog.FileName
                SaveFile(FileName)
            End If
        Else
            Try
                File.Delete(filePath)
                SaveFile(filePath)
            Catch ex As Exception
                MessageBox.Show("This file have used by another program.")
            End Try

        End If
    End Sub

    'Save data from datatable to xml file with ext .ns
    Private Sub SaveFile(ByVal FileName As String)

        Try
            filePath = FileName
            ' Write node root for file meetingroom.qt
            Dim WriterQT As New StreamWriter(FileName, False, System.Text.Encoding.Unicode)
            WriterQT.WriteLine("<RootPersonal>")
            WriterQT.WriteLine("</RootPersonal>")
            WriterQT.Close()
            ' Write data to file
            xmldoc.Load(FileName)
            'nút gốc là Subject
            Dim inColumn As Integer = dataTable.Columns.Count()
            For Each rows As DataRow In dataTable.Rows
                Dim root As XmlElement = xmldoc.CreateElement("Personal")
                For i As Integer = 0 To inColumn - 1
                    Dim childName As XmlElement = xmldoc.CreateElement(dataTable.Columns.Item(i).ColumnName)
                    childName.AppendChild(xmldoc.CreateTextNode(rows.Item(i)))
                    root.AppendChild(childName)
                Next
                'thêm vào file xml
                xmldoc.DocumentElement.AppendChild(root)
            Next
            'lưu lại file xml
            xmldoc.Save(FileName)
            MessageBox.Show("Save success")
        Catch ex As Exception
            MessageBox.Show("Have error in process.Please try agian")
        End Try

    End Sub

    'Load data from file to Datatable
    Public Sub LoadXMLForDatable(ByVal FilePath As String)
        Try
            xmldoc.Load(FilePath)
            Dim nodeList As XmlNodeList = xmldoc.GetElementsByTagName("Personal")
            For Each node As XmlNode In nodeList
                Dim i As Integer = 0
                dataRow = dataTable.NewRow()
                For Each nodechild As XmlNode In node.ChildNodes
                    dataRow.Item(i) = nodechild.Value
                    i += 1
                Next
                dataTable.Rows.Add(dataRow)
            Next
            DataGridView_Personal.DataSource = dataTable
        Catch ex As Exception
            MessageBox.Show("Have a error in open file process.Please try again")
        End Try
    End Sub
End Class